//
//  VCTableViewDelegate.m
//  TestForReactiveCocoa
//
//  Created by liangshangjia on 16/2/17.
//  Copyright © 2016年 Sami-Network. All rights reserved.
//

#import "VCTableViewDelegate.h"
#import "TableViewCell.h"
#import "VCViewModel.h"

@interface VCTableViewDelegate ()

@property (nonatomic, assign) VCViewModel *viewModel_;
@end

@implementation VCTableViewDelegate

#pragma mark - Override
+ (instancetype)delegateWithViewModel:(BaseViewModel *)viewModel andTableView:(UITableView *)tableView
{
    VCTableViewDelegate *delegate = [super delegateWithViewModel:viewModel andTableView:tableView];
    [TableViewCell registerCellToTableView:tableView];
    delegate.viewModel_ = (VCViewModel *)delegate.viewModel;
    tableView.rowHeight = [TableViewCell cellHeightWithEntity:nil atIndex:0];
    return delegate;
}


#pragma mark - Datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.viewModel_.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableViewCell *cell = [TableViewCell cellWithTableView:tableView];
    [cell configureCellWithEntity:self.viewModel_.array[indexPath.row]];
    return cell;
}



#pragma mark - Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UINavigationController *navi = tableView.viewController.navigationController;
    UIViewController *nv = [[UIViewController alloc]init];
    nv.view.backgroundColor = [UIColor whiteColor];
    !navi ?: [navi pushViewController:[UIViewController new] animated:YES];
}


@end
